iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0
Software Development

十年後重讀作業系統恐龍本系列 第 6

ch3圖3.20,21,22-客戶—伺服器系統的通信:使用socket的通信

  • 分享至 

  • xImage
  •  

插座(socket)例子:日期伺服器&客戶端。

名詞定義:

  • 插座 (socket):通信的終端。一組行程使用一對插座,雙方各一個,在網路上通信。一個插座是由一個 IP 位址和一個埠號碼 (port number) 所組成。
  • 伺服器藉由傾聽某一特定 port 來等待進入的客戶要求。一旦要求被接受之後,伺服器端就接受客戶端插座的連接來完成連接。

以下例子以 java 的連接傾向插座 [connection-oriented (TCP) socket],要用 Socket 類別製作。
DateServer.java

/**
 * Time-of-day server listening to port 6013.
 *
 * Figure 3.21
 *
 * @author Silberschatz, Gagne, and Galvin. 
 * Operating System Concepts  - Ninth Edition
 * Copyright John Wiley & Sons - 2013.
 */
 
import java.net.*;
import java.io.*;

public class DateServer
{
	public static void main(String[] args)  {
		try {
            /* 創建一個 ServerSocket 對象,並在端口 6013 上監聽進入的連接請求 */
			ServerSocket sock = new ServerSocket(6013); 

			// now listen for connections
			while (true) {
                /* 這行代碼調用 accept() 方法,這會阻塞並等待客戶端連接。一旦有客戶端連接,將返回一個 Socket 對象,表示與該客戶端的連接 */
				Socket client = sock.accept();
				// we have a connection
				
                /* 這行代碼獲取與客戶端連接的輸出流,並創建一個 PrintWriter 對象,這樣可以方便地向客戶端發送文本數據。第二個參數 true 使得自動刷新輸出流 */
				PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
				// write the Date to the socket
                /* 媽,我在這裡!創建一個 Date 對象(表示當前日期和時間),並將其轉換為字符串後發送給客戶端 */
				pout.println("Mary here!" + new java.util.Date().toString());

				// close the socket and resume listening for more connections
				client.close();
			}
		}
		catch (IOException ioe) {
				System.err.println(ioe);
		}
	}
}

DateClient.java

/**
 * Client program requesting current date from server.
 *
 * Figure 3.22
 *
 * @author Silberschatz, Gagne and Galvin
 * Operating System Concepts  - Eighth Edition
 */ 

import java.net.*;
import java.io.*;

public class DateClient
{
	public static void main(String[] args)  {
		try {
			// this could be changed to an IP name or address other than the localhost
			Socket sock = new Socket("127.0.0.1",6013);
			InputStream in = sock.getInputStream();
			BufferedReader bin = new BufferedReader(new InputStreamReader(in));

			String line;
			while( (line = bin.readLine()) != null)
				System.out.println(line);
				
			sock.close();
		}
		catch (IOException ioe) {
				System.err.println(ioe);
		}
	}
}

Terminal
安裝 java

sudo apt update
sudo apt install openjdk-17-jdk

確認 java 安裝成功並編譯 .java 檔案,生成相應的 .class 文件
https://ithelp.ithome.com.tw/upload/images/20240920/20168766OOlqgpuLT4.png

java -version
javac DateServer.java DateClient.java

分別在一個 Terminal 執行 Server

java DateServer

在另一個 Terminal 執行 Client

java DateClient

結果

  • 通信成功
    https://ithelp.ithome.com.tw/upload/images/20240920/20168766HZfWQAYo3V.png
  • 通信失敗 (Server 關掉的情況下)
    https://ithelp.ithome.com.tw/upload/images/20240920/20168766IYQl40M8Y2.png

參考:


上一篇
ch3圖3.17,18-說明POSIX共用記憶體API的行程
下一篇
ch3圖3.23-客戶—伺服器系統的通信:遠程程序呼叫(RPC)
系列文
十年後重讀作業系統恐龍本12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言